<?php
/**
 * APPDANCE 初始化处理程序
 */
header("Content-Type: text/html;charset=UTF-8");
error_reporting(0);// 设置报错等级为0
!defined('DANCE') && die('Access Error!');
!defined('ROOT') && define('ROOT', dirname(dirname(__FILE__)) . '/');
!defined('ADPATH') && define('ADPATH', ROOT . basename(dirname(__FILE__)) . '/');

function_exists('date_default_timezone_set') && date_default_timezone_set('Etc/GMT+0');

$AD = __CONFIG();

if ($AD['PHPDEBUG'] == 1) {
	error_reporting(7);// 设置报错等级为7
}

__AUTO(array('input', 'putout', 'router', 'page', 'time', 'safe'));
// 获取应用配置
$APP = appInit(explode($AD['routesplit'], aInput::init($AD['route'])));
// 处理数据库信息
if (isset($APP['config']['datatype']) && isset($APP['config']['dataconn'])) {
	__AUTO(array('data'));
	aData::conn($APP['config']['datatype'], $APP['config']['dataconn']);
} 
// 初始化时间
$timestamp = aTime::now();
// 路由处理
aRouter::init($AD);
/**
 * 自动加载类文件
 * $class array('类名'=>'文件名') 或者 array('既是类名也是文件名')
 * $prognew 进行new操作
 */
function __AUTO($class = array(), $donew = true) {
	if (empty($class)) {
		return;
	} 
	foreach($class as $key => $value) {
		$classname = "";
		if (!is_numeric($key)) {
			$classname = $key;
		} else {
			$classname = $value;
		} 
		if (file_exists(ADPATH . "def/" . $value . ".php")) {
			require ADPATH . "def/" . $value . ".php";
		} elseif (file_exists(ADPATH . "core/" . $value . ".php")) {
			require ADPATH . "core/" . $value . ".php";
			$donew == true && $GLOBALS['_cls_' . strtoupper($classname)] = new $classname;
		} 
	} 
	return;
} 

/**
 * 获取配置信息
 * $appname 应用名
 * $content 配置内容
 */
function __CONFIG($appname = '', $content = '') {
	$config = array();
	if (!$appname) {
		$cfgroot = ADPATH . "cfg";
	} else {
		$cfgroot = ROOT . 'apps/' . $appname;
	} 

	if ($content) {
		$_config = $content;
	} elseif (file_exists($cfgroot . '/_config.php')) {
		$_config = str_replace(array("\r\n", "\r", "\n", "\t"), '', file_get_contents($cfgroot . '/_config.php'));
	} else {
		$_config = '';
	} 
	if ($_config) {
		$_config = preg_replace(array('~#.*?#~', "~<\?.*?\?>~"), '', $_config);
		$arr = 0;
		if (strpos($_config, '{') !== false) {
			preg_match_all("~(\w+)\s*{(.*?)}~", $_config, $_mconfig);
			foreach($_mconfig['1'] as $key => $value) {
				$arrconfig[$value] = __CONFIG($appname, $_mconfig['2'][$key]);
				$_config = str_replace($_mconfig['0'], '', $_config);
			} 
			$arr = 1;
		} 
		$_configdb = explode(';', $_config);
		foreach($_configdb as $_cfg) {
			$_cfg = trim($_cfg);
			if ($_cfg) {
				if(strpos($_cfg,'=>') === false) {
					die('Config error.');
				}
				list($_key, $_value) = explode('=>', $_cfg);
				/*//TODO 检测配置参数合法性
				if (preg_replace("~\s*\w+\s*~",'',$_key)) {
					die('Config error.');
				}*/
				trim($_key) && $config[trim($_key)] = trim($_value);
			} else {
				continue;
			} 
		} 

		if ($arr == 1) {
			if (!empty($config)) {
				$config = array_merge($config, $arrconfig);
			} else {
				$config = $arrconfig;
			} 
		} 
	} 
	return $config;
} 

/**
 * 应用初始化
 */
function appInit($array) {
	global $AD;
	$m = $a = $appname = '';
	if ($array['0']) { // 可能为空
		$m = $array['0'];
	} else {
		$m = $AD['defaultmodule'];
	} 

	if (isset($array['1'])) { // 可能没有设置
		$c = $array['1'];
	} else {
		$c = $AD['defaultcontroler'];
	} 

	$appname = aInput::init('appname');
	if ($appname) {
		$name = $appname;
	} else {
		$name = $AD['defaultapp'];
	} 

	$config = __CONFIG($name); // 获取应用的配置信息
	
	return array('name' => $name, 'm' => $m, 'c' => $c, 'config' => $config);
} 

?>